package com.tweetbook.Persistence;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.tweetbook.entity.Account.Account;
import com.tweetbook.resources.DBConnection;

public class AccountServer {

	Account acc;
	DBConnection db = new DBConnection();
	

	public AccountServer(Account acc){
		this.acc = acc;
	}
	
	public void addAccount(){
		String qry = "INSERT INTO account(email,passwd,name)" +
			 		 "VALUES('"+acc.getEmail()+"','"+acc.getPasswd()
			 		 +"','"+acc.getName()+"')";
		System.out.println(qry);
		db.insertQry(qry);
		retrieveAccount();
		addUser();
	}
	
	protected void addUser(){
		String qry = "INSERT INTO user(user_id,wall_id,profile_id)"+
					 "VALUES("+acc.getAccount_id()+",-1,-1)";
		db.insertQry(qry);
	}
	
	public void retrieveAccount(){
		String qry = "SELECT account_id " +
					 "FROM account WHERE email ='"+acc.getEmail()
					 +"' and passwd ='"+acc.getPasswd()+"'";
		
		ResultSet rs = db.selectQry(qry);
		if(rs==null){
			System.out.println("no account id retrieved");
		}
		try {
			while(rs.next()){
				acc.setAccount_id(rs.getInt("account_id"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void retrieveAccount(int accountID){
		String qry = "SELECT email,passwd,name"+
					 "FROM account "+
					 "WHERE account_id="+accountID;
		ResultSet rs = db.selectQry(qry);
		if(rs==null){
			System.out.println("no account id retrieved");
		}
		try {
			while(rs.next()){
				acc.setName(rs.getString("name"));
				acc.setEmail(rs.getString("email"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
